﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestLink2Net.Entities;
using System.Security.Principal;

namespace TestLink2Net.Security
{
    public class RightPrincipal : IPrincipal
    {
        public RightPrincipal(IPrincipal user)
        {
            _user = user;
        }

        public RightPrincipal(IPrincipal user, List<Right> rights)
            : this(user)
        {
            _rights = rights;
        }

        private IPrincipal _user = null;
        private List<Right> _rights = new List<Right>();

        public bool HasRight(String rightName)
        {
            bool result = false;
            if (_user is RightPrincipal)
            {
                result = ((RightPrincipal)_user).HasRight(rightName);
            }

            if (!result)
            {
                result = _rights.Count(r => r.RightName == rightName) > 0;
            }

            return result;
        }

        #region IPrincipal Members

        public IIdentity Identity
        {
            get { return _user.Identity; }
        }

        public bool IsInRole(string role)
        {
            return _user.IsInRole(role);
        }

        #endregion
    }

}
